package threadDemo;
/**
 * Array是线程不安全的 使用synchronized让集合ArrayList线程安全
 */

import java.util.ArrayList;
import java.util.List;

public class Test17 {
    public static void main(String[] args) throws InterruptedException {
        List<String> l = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            new Thread(()->{
                synchronized (l){
                    l.add(Thread.currentThread().getName());
                }
            },String.valueOf(i)).start();
        }
        Thread.sleep(3000);
        System.out.println(l.size());
    }

}
